 package com.gmrz.uaf.protocol.v1.validaton;
 
 import com.gmrz.uaf.common.Constants;
 import com.gmrz.uaf.common.GuiceUtil;
 import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
 import com.gmrz.uaf.server.ServerConfig;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
 public class AppIDValidator
   implements Validator<String>
 {
   private static final Logger LOG = LogManager.getLogger(AppIDValidator.class);
   private String expectedAppID;
   private String tenantId;
   private String transType;
 
   public AppIDValidator(String expectedAppID,String tenantId,String transType)
   {
     this.expectedAppID = expectedAppID;
     this.tenantId = tenantId;
     this.transType = transType;
   }
 
   public void validate(String actualAppID) throws HeaderValidationException
   {
       String actualAppIDParam = new String(actualAppID);
       ServerConfig serverConfig = GuiceUtil.getProcessorInjector().getInstance(ServerConfig.class);
       if(serverConfig.getAppIdUrlBoolean(tenantId,transType)) {
           int end = actualAppID.lastIndexOf("/");
           String appIDEndStr = actualAppID.substring(end);
           String appIdTemp = actualAppID.substring(0,end);
           int start = appIdTemp.lastIndexOf("/");
           String appIDBeforeStr = appIdTemp.substring(0,start);
           actualAppID = appIDBeforeStr+"/"+ Constants.APP_ID + appIDEndStr;
           LOG.debug("paramappID:"+actualAppIDParam+",appID:"+actualAppID);
       }
     if ((actualAppID == null) || (!this.expectedAppID.equals(actualAppID))) {
       LOG.error("AppID[" + actualAppID + "] doesn't matches with the value present in header[" + this.expectedAppID + "]");
       throw new HeaderValidationException(UAFErrorCode.PROTOCOL_APPID_VALIDATION_FAILED);
     }
   }
 }
